<?php
use \think\Request;

/**
 * @param $expTitle 名称
 * @param $headArr 参数
 * @param $expTableData 内容
 * @throws \PHPExcel_Exception
 * @throws \PHPExcel_Reader_Exception
 */
function exportExcel($expTitle, $headArr, $data)
{
    $fileName = $expTitle . '_' . date('YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
    vendor("phpoffice.phpexcel.Classes.PHPExcel");

    $objPHPExcel = new \PHPExcel();

    //设置表头
    $key = ord("A");
    foreach ($headArr as $v) {
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
        $key += 1;
    }

    $column = 2;
    $objActSheet = $objPHPExcel->getActiveSheet();
    foreach ($data as $key => $rows) { //行写入
        $span = ord("A");
        foreach ($rows as $keyName => $value) { // 列写入
            $j = chr($span);
            if ($keyName == "id") {
                $objActSheet->setCellValue($j . $column, $column - 1);
            } else {
                $objActSheet->setCellValue($j . $column, $value);
            }
            $span++;
        }
        $column++;
    }

    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName . '.xls"');
    header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //Excel5为xls格式，excel2007为xlsx格式
    $objWriter->save('php://output');
    exit;
}

function apicurl($url, $params = false, $ispost = 0)
{
    $httpInfo = array();
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === false) {
        // echo "cURL Error: " . curl_error($ch);die;
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

function comm_auth_check($action)
{
    $request = Request::instance();
    $model = $request->module();
    $controller = $request->controller();
    $ruleName = strtolower($model . "/" . $controller . "/" . config($action));

    return cmf_auth_check(cmf_get_current_admin_id(), $ruleName);
}

function express($com, $num)
{
    //参数设置
    $post_data = array();
    $post_data["customer"] = config('kuaidi.customer');
    $key = config('kuaidi.key');
    $post_data["param"] = '{"com":"'.$com.'","num":"'.$num.'"}';

    $url = 'http://poll.kuaidi100.com/poll/query.do';
    $post_data["sign"] = md5($post_data["param"] . $key . $post_data["customer"]);
    $post_data["sign"] = strtoupper($post_data["sign"]);
    $o = "";
    foreach ($post_data as $k => $v) {
        $o .= "$k=" . urlencode($v) . "&"; //默认UTF-8编码格式
    }
    $post_data = substr($o, 0, -1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $result = curl_exec($ch);
    $data = str_replace("\"", '"', $result);
    $data = json_decode($data, true);
    return $data;
}
